package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.webkit.CookieManager;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.jpush.JpushSetAlisTagUtil;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.login.ui.LoginActivity;
import com.uchengwang.ui.utils.ActivityUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.umeng.analytics.MobclickAgent;

import io.rong.imkit.RongIM;


/**
 * Created by U城网 on 2017/6/12.
 * 我的设置
 */

public class MySettingActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private RelativeLayout msg_rl, pri_rl, help_rl, cache_rl, desc_rl, feedback_rl, set_link_rl, set_release_rl;
    private Button exit_bt;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_setting);
        setTitle("设置");

        initView();
        
    }
    
    private void initView(){
        msg_rl = (RelativeLayout) findViewById(R.id.set_msg_rl);
        msg_rl.setOnClickListener(this);

        set_release_rl = (RelativeLayout) findViewById(R.id.set_release_rl);
        set_release_rl.setOnClickListener(this);

        pri_rl = (RelativeLayout) findViewById(R.id.set_pri_rl);
        pri_rl.setOnClickListener(this);
        help_rl = (RelativeLayout) findViewById(R.id.set_help_rl);
        help_rl.setOnClickListener(this);
        cache_rl = (RelativeLayout) findViewById(R.id.set_cache_rl);
        cache_rl.setOnClickListener(this);
        desc_rl = (RelativeLayout) findViewById(R.id.set_desc_rl);
        desc_rl.setOnClickListener(this);
        exit_bt = (Button) findViewById(R.id.set_exit_bt);
        exit_bt.setOnClickListener(this);

        feedback_rl = (RelativeLayout) findViewById(R.id.set_feedback_rl);//意见反馈
        feedback_rl.setOnClickListener(this);

        set_link_rl = (RelativeLayout) findViewById(R.id.set_link_rl);//关联账号
        set_link_rl.setOnClickListener(this);

        if (UChengApplication.userRole == BaseConfig.CLUB_ROLE){
            //转让社团管理权
//            exit_bt.setText("转让管理权");
//            exit_bt.setTextColor(getResources().getColor(R.color.color_red));
            pri_rl.setVisibility(View.GONE);
            cache_rl.setVisibility(View.GONE);
        }else {
            set_link_rl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;

        switch (view.getId()){
            case R.id.set_msg_rl:
                intent = new Intent(this, MsgSettingActivity.class);
                break;

            case R.id.set_release_rl:
                intent = new Intent(this, MyPrivateSetActivity.class);
                break;

//            case R.id.set_pri_rl:
//                intent = new Intent(this, MyPrivateSetActivity.class);
//                break;
            case R.id.set_help_rl://帮助
                intent = new Intent(this, LoadWebActivity.class);
                intent.putExtra(BaseConfig.TYPE, 1);
                break;
            case R.id.set_cache_rl:
                UCommonUtil.getUtil().showToast(this, "清除成功");
                break;
            case R.id.set_desc_rl://关于我们 u城网
                intent = new Intent(this, LoadWebActivity.class);
                intent.putExtra(BaseConfig.TYPE, 2);
                break;
            case R.id.set_feedback_rl://意见反馈
                intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/feedback");
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                intent.putExtra(BaseConfig.URL_TITLE, "意见反馈");

//                intent = new Intent(this, FeedbackItemActivity.class);
                break;
            case R.id.set_link_rl://关联账号
                intent = new Intent(this, LinkAccountActivity.class);
                break;

            case R.id.set_exit_bt://退出登录
//                if (UChengApplication.userRole == BaseConfig.CLUB_ROLE){
//                    //转让社团管理权
//                    intent = new Intent(this, TransferCodeActivity.class);
//                }else {
                    //退出
                    dialog = DialogUtils.getUtils().showDialogAll(this, false, "", "确定退出Uni?", "再玩玩", "确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DialogUtils.getUtils().cancel();
                            exitUcw();
                        }
                    }, null);
//                }

                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }

    /**
     * 退出U城网
     * */
    private void exitUcw(){
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();

        RongIM.getInstance().logout();//退出

        MobclickAgent.onKillProcess(MySettingActivity.this);//友盟统计

        JpushSetAlisTagUtil.getUtil().setNullAlis(this);

        SPUtils.getPreferences(MySettingActivity.this).setAccountPw("");
        SPUtils.getPreferences(MySettingActivity.this).setToken("");
        SPUtils.getPreferences(MySettingActivity.this).putInt(BaseConfig.USER_ROLE, 0);
        SPUtils.getPreferences(MySettingActivity.this).setRyToken("");//融云token置空
        SPUtils.getPreferences(MySettingActivity.this).setLinkMainToken("");
        SPUtils.getPreferences(this).putLong(BaseConfig.UNI_ID, 0);

        LiteOrmDBUtil.getLiteOrm(MySettingActivity.this).exit();

        UChengApplication.TOKEN = "";
        UChengApplication.uid = 0;
        UChengApplication.nickName = "";
        UChengApplication.uniId = 0;
        UChengApplication.myUniId = 0;
        UChengApplication.schoolName = "";
        UChengApplication.mySchoolName = "";
        UChengApplication.userAvatar = "";
        UChengApplication.userRole = 0;
        UChengApplication.isAuthen = false;

        // TODO Auto-generated method stub
        ActivityUtils.getUtils().overLoginActivity();

        Intent intent = new Intent(MySettingActivity.this, LoginActivity.class);
        startActivity(intent);

        MySettingActivity.this.finish();

    }
}
